package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//注册界面还要写一个连接到登录界面的
@Gomoku_Component("注册界面")
public class Register {
    public static void main(String[]args){
        JFrame frame = new JFrame("注册");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(400,300,600,400);
        frame.setBackground(Color.WHITE);

        @Interface_component(name = "主窗口框架，使用BorderLayout布局")
        JPanel RegisterPanel = new JPanel(new BorderLayout());
        RegisterPanel.setBackground(Color.gray);
        RegisterPanel.setBorder(BorderFactory.createEmptyBorder(80,80,100,100));

        @Interface_component(name = "表单面板设置注册的，使用GridLayout布局")
        JPanel  formPanel = new JPanel(new GridLayout(2,2,0,10));
        formPanel.setBackground(Color.red);
        formPanel.setBorder(BorderFactory.createEmptyBorder(60,10,10,130));
        RegisterPanel.add(formPanel);


        @Interface_component(name = "注册按钮面板，使用FlowLayout布局")
        JPanel ButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,0,0));
        ButtonPanel.setBackground(Color.green);
        ButtonPanel.setBorder(BorderFactory.createEmptyBorder(0,55,10,50));
        RegisterPanel.add(ButtonPanel,BorderLayout.SOUTH);

        @Interface_component(name = "用户名标签")
        JLabel Label_username = new JLabel("用户名");
        Label_username.setBorder(BorderFactory.createEmptyBorder(20,80,20,0));
        JTextField username = new JTextField(15);

        @Interface_component(name = "密码标签")
        JLabel Label_password = new JLabel("密码");
        Label_password.setBorder(BorderFactory.createEmptyBorder(5,80,5,0));
        JPasswordField password = new JPasswordField(15);

        @Interface_component(name = "注册按钮")
        JButton btnRegister = new JButton("注册");
        btnRegister.setBackground(new Color(70, 130, 180));
        btnRegister.setForeground(Color.WHITE);
        btnRegister.setFont(new Font("宋体", Font.BOLD, 14));
        btnRegister.setFocusPainted(false);
        btnRegister.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(39, 174, 96), 6),
                BorderFactory.createEmptyBorder(8, 23, 8, 23)));
        ButtonPanel.add(btnRegister);

        btnRegister.addActionListener(new ActionListener(){
                @Override
                @Gomoku_Event(event = "注册按钮点击事件")
                public void actionPerformed(ActionEvent e){
                    String usernameText = username.getText();
                    String passwordText = new String(password.getPassword());
                    System.out.println(usernameText);
                    System.out.println(passwordText);
                    System.out.println("注册");
                    if(usernameText.isEmpty() || passwordText.isEmpty()){
                        JOptionPane.showMessageDialog(frame, "请输入用户名和密码");
                        return;
                    }

                    try{
                        record(usernameText,passwordText);
                        Login login = new Login();
                        frame.dispose();
                        login.main(args);

                    }catch(SQLException ex){
                        ex.printStackTrace();
                    }

                }

                @Interface_event(event = "将用户名密码插入数据库")
                public void record(String username,String password)throws SQLException{
//                      Statement不能处理参数占位符?，所以报错
                    // PreparedStatement和Statement的区别：
                    // PreparedStatement可以处理参数占位符?，所以可以防止SQL注入攻击
                    //ID设为自增主键
                        Mysql conn = new Mysql();;
                        conn.getConnection();
                        String sql = "insert into admin(username,password) values(?,?)";
                        PreparedStatement ps = conn.con.prepareStatement(sql);
                        ps.setString(1,username);
                        ps.setString(2,password);

                        int rows = ps.executeUpdate();
                        System.out.println(rows+"affected");

                        if(ps!=null)
                            ps.close();
                        if(conn!=null)
                            conn.con.close();


                    }


        });


        formPanel.add(Label_username);
        formPanel.add(username);
        formPanel.add(Label_password);
        formPanel.add(password);
        frame.add(RegisterPanel);

        frame.setVisible(true);
    }


}
